home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 19 / develop 19 code / Color Picker Sample / App.c next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  5.7 KB  |  271 lines  |  [TEXT/KAHL]

  1. #include "AppInterface.h"
  2. #include "ColorPicker.h"
  3. #include "GestaltEqu.h"
  4.  
  5. #define        kPickerMenuID    131
  6. #define            iModalPicker    1
  7. #define            iModelessPicker    2
  8. #define            iAppPicker        3
  9. #define            iPickerPicker    4
  10.  
  11.  
  12. /* The one shell global we need */
  13. extern MenuHandle    gShellMenuHandles[];
  14.  
  15. ////////////////
  16. // Globals
  17.  
  18. WindowRecord        gWStore;
  19. MenuHandle            gPickerMenuHandle = nil;
  20.  
  21. extern RGBColor        myRGBColor;
  22. extern picker        myPicker;        // Global picker reference.
  23. extern WindowPtr     myDocWindow;
  24. extern DialogPtr     myDialog;
  25.  
  26. /*  A chance to pre-handle an event */
  27. Boolean AppDoEvent(EventRecord *event)
  28. {
  29.     Boolean        handled = false;
  30.     
  31.     Boolean SampleDoEvent(EventRecord *event);
  32.     
  33.     handled = SampleDoEvent(event);
  34.     return handled;
  35. }
  36.  
  37. /* Called by the Shell at startup time */
  38. Boolean AppInit(void)     /* returns false if initialization fails */
  39. {
  40.     Rect    tempRect = {0, 0, 64, 64};
  41.     long    gestaltResult;
  42.     
  43.     // Beep if Color picker not installed
  44.     if(Gestalt(gestaltColorPicker, &gestaltResult) != noErr)
  45.     {
  46.         SysBeep(10);
  47.         return false;
  48.     }
  49.     
  50.     OffsetRect(&tempRect, 30, GetMBarHeight() + 40);
  51.     myDocWindow = NewCWindow(&gWStore, &tempRect, "\pColor", true,
  52.                         documentProc, (WindowPtr)(-1), false, 0L);
  53.     
  54.     /* Set up picker menu */
  55.     gPickerMenuHandle = GetMenu(kPickerMenuID);
  56.     if(gPickerMenuHandle == nil)
  57.     {
  58.         SysBeep(10);
  59.         return false;
  60.     }
  61.     (*gPickerMenuHandle)->menuID = kPickerMenuID;
  62.     InsertMenu(gPickerMenuHandle, 0);
  63.     
  64.     /* Update the menu bar */
  65.     DrawMenuBar();
  66.     return true;
  67. }
  68.  
  69. /* Called when the shell receives an Activate event. */
  70. void AppActivate(WindowPtr wind, Boolean activate)
  71. {
  72. }
  73.  
  74. /* Called when a window needs updating. BeginUpdate() has already been called, and the 
  75. port is set to the appropriate window */
  76. void AppUpdate(EventRecord *event)
  77. {
  78.     void DoTheUpdate(WindowPtr aWindow);
  79.     
  80.     if((WindowPtr) event->message == myDocWindow)
  81.     {
  82.         RGBForeColor(&myRGBColor);
  83.         PaintRect(&myDocWindow->portRect);
  84.     }
  85.     else
  86.         DrawDialog((WindowPtr) event->message);
  87. }
  88.  
  89. /* Called when the shell recieves a null event. */
  90. void AppIdle(EventRecord *Event)
  91. {
  92. }
  93.  
  94. /* Called when there is a click in the content of a window. The port is already set to 
  95. the window, and thePt is in local coords. */
  96. void AppClick(Point thePt, WindowPtr whichWindptr, Boolean doubleClick)
  97. {
  98. }
  99.  
  100. /* Called when there is a click in the grow region. */
  101. void AppGrowWindow(WindowPtr wind, Point where, Rect *desk)
  102. {
  103. }
  104.  
  105. /* Called when the user clicks in the zoom box of a window. */
  106. void     AppZoomWindow(WindowPtr wind, short zoomDir)
  107. {
  108. }
  109.     
  110. /* Called when there is a click in the menu bar, before the menu is shown. This is
  111. the app's opportunity to enable and disable menu items. */
  112. void    AppAdjustMenus()
  113. {
  114. }    
  115.  
  116. /* called when a menu other than Apple, File, or Edit is used. */
  117. void AppMenu(short id, short item)
  118. {
  119.     void     PickAColor(void);
  120.     OSErr     BuildModelessSysDialog(void);
  121.     OSErr     BuildAppDialog(void);
  122.     OSErr     BuildPickerDialog(void);
  123.     
  124.     OSErr    err = noErr;
  125.     
  126.     if(id != kPickerMenuID)
  127.         return;
  128.     
  129.     // Modal is a special case
  130.     if(item == iModalPicker)
  131.     {
  132.         PickAColor();
  133.         return;
  134.     }
  135.         
  136.     switch(item)
  137.     {
  138.         case iModelessPicker:
  139.             err = BuildModelessSysDialog();
  140.             break;
  141.         
  142.         case iAppPicker:
  143.             err = BuildAppDialog();
  144.             break;
  145.         
  146.         case iPickerPicker:
  147.             err = BuildPickerDialog();
  148.             break;
  149.         
  150.         default:
  151.             break;
  152.     }
  153.     
  154.     // If no error, set picker up and show it, disabling the picker menu
  155.     if(err == noErr && myPicker != nil)
  156.     {
  157.         PMColor myPMColor;
  158.  
  159.         myPMColor.color.rgb = myRGBColor;
  160.         myPMColor.profile = 0L;
  161.  
  162.         SetPickerColor(myPicker, kOriginalColor, &myPMColor);
  163.         SetPickerColor(myPicker, kNewColor, &myPMColor);
  164.         SetPickerPrompt(myPicker, "\pModeless Picker");
  165.         SetPickerVisibility(myPicker, true);
  166.         if(item == iAppPicker && myDialog != nil)
  167.             ShowWindow(myDialog);
  168.         
  169.         // Fix menus
  170.         HiliteMenu(0);
  171.         DisableItem(gPickerMenuHandle, 0);
  172.         DrawMenuBar();
  173.     }
  174.     else
  175.         SysBeep(10);    
  176. }
  177.  
  178. /* Called when the user selects "New" from the File menu. A no-op right now */            
  179. void    AppNew(void)
  180. {
  181. }
  182.  
  183. /* Called when the user selects "Open" from the File menu. */
  184. void    AppOpen(void)
  185. {
  186. }
  187.  
  188.  
  189. /* Called when the user selects "Close" from the File menu or clicks the close box 
  190. of a window. */            
  191. Boolean    AppClose(void)
  192. {
  193.     /* returns false if the user cancels the save */
  194.     
  195.     // Must be the app dialog, since that's the only thing with a close box
  196.     void CloseAppPicker(void);
  197.     
  198.     CloseAppPicker();
  199.     EnableItem(gPickerMenuHandle, 0);
  200.     DrawMenuBar();
  201.     
  202.     return true;
  203. }
  204.  
  205. /* Called when the user selects "Save" from the File menu. */
  206. Boolean    AppSave(void)
  207. {
  208.     /* returns false if the user cancels the save */
  209.     return true;
  210. }
  211.  
  212. /* Called when the user selects "Save As..." from the File menu. */
  213. Boolean    AppSaveAs(void)
  214. {
  215.     /* returns false if the user cancels the save */
  216.     return true;
  217. }
  218.  
  219. /* Called when the user selects "Page Setup..." from the File menu. */
  220. void AppPageSetup(void)
  221. {
  222. }
  223.  
  224. /* Called when the user selects "Print..." from the File menu. */
  225. void AppPrint(void)
  226. {
  227. }
  228.  
  229. /* Called when the user selects "Undo" from the Edit menu. */
  230. void AppUndo(void)
  231. {
  232. }
  233.  
  234. /* Called when the user selects "Cut" from the Edit menu. */
  235. void AppCut(void)
  236. {
  237. }
  238.  
  239. /* Called when the user selects "Copy" from the Edit menu. */
  240. OSErr AppCopy(void)
  241. {
  242.     return noErr;
  243. }            
  244.             
  245. /* Called when the user selects "Paste" from the Edit menu. */
  246. void AppPaste(void)
  247. {
  248. }
  249.  
  250. /* Called when the user selects "Clear" from the Edit menu. */
  251. void AppClear(void)
  252. {
  253. }
  254.  
  255. /*     Called when the user chooses "Quit" from the File menu. If the user cancels
  256. the save it returns false, otherwise it returns true and the shell quits */
  257. Boolean AppQuit(void)
  258. {
  259.     /* returns false if the user cancels the save at any point */
  260.     return true;
  261. }
  262.  
  263. /* Called when the shell is about to quit, just deallocates memory. */
  264. void AppCleanUp(void)
  265. {
  266.     if(myDocWindow != nil)
  267.         CloseWindow(myDocWindow);
  268. }
  269.  
  270.  
  271.